﻿using Common;
using Model.User;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace DataService.User
{
    /// <summary>
    /// AccountData 的摘要说明
    /// </summary>
    public class AccountInfo
    {
        /// 保存账号，并返回主键id
        /// <summary>
        /// 保存账号，并返回主键id
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string SaveAccount(AccountParam param)
        {
            string sqlStr = @"insert into Account( DeviceId, PlatOS, ChannelId, ChannelName ) values ( ?DeviceId, ?PlatOS, ?ChannelId, ?ChannelName ); 
                              select @@Identity";
            MySqlParameter[] sqlParam = new MySqlParameter[] {
                new MySqlParameter("?DeviceId", param.DeviceId ?? ""),
                new MySqlParameter("?PlatOS", param.PlatOS),
                new MySqlParameter("?ChannelId", param.ChannelId ?? ""),
                new MySqlParameter("?ChannelName", param.ChannelName ?? ""),
            };
            var res = MySQLDataControl.ExecuteScalar(MySQLDataControl.GetConnection(), CommandType.Text, sqlStr, sqlParam);
            return res != null ? res.ToString() : "";
        }

        /// 保存登录日志
        /// <summary>
        /// 保存登录日志
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static bool SaveLoginLog(string uid, string token)
        {
            var dateNow = DateTime.Now;
            string sqlStr = @"insert into LoginLog( UID, SubmitTime, TimeStamp, DataFlag, Token, Ip ) values ( ?UID, ?SubmitTime, ?TimeStamp, ?DataFlag, ?Token, ?Ip )";
            MySqlParameter[] sqlParam = new MySqlParameter[] {
                new MySqlParameter("?UID", uid),
                new MySqlParameter("?SubmitTime", dateNow),
                new MySqlParameter("?TimeStamp", CheckInfo.ConvertTimeToUTCTimeStamp(dateNow)),
                new MySqlParameter("?DataFlag", 1),
                new MySqlParameter("?Token", token),
                new MySqlParameter("?Ip", CheckInfo.GetClientIP())
            };
            return MySQLDataControl.ExecuteNonQuery(MySQLDataControl.GetConnection(), CommandType.Text, sqlStr, sqlParam) > 0;
        }

        /// 根据渠道id或者设备id获取UID
        /// <summary>
        /// 根据渠道id或者设备id获取UID
        /// </summary>
        /// <param name="channel_id"></param>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public static string GetUIDByChannelIdOrDeviceId(string channel_id, string deviceId)
        {
            string result = string.Empty;
            string sqlStr = "";
            if (!string.IsNullOrEmpty(channel_id))
            {
                sqlStr = @"select Id from Account where ChannelId = ?ChannelId ";
            }
            else if (!string.IsNullOrEmpty(deviceId))
            {
                sqlStr = @"select Id from Account where DeviceId = ?DeviceId ";
            }
            MySqlParameter[] sqlParam = new MySqlParameter[] {
                new MySqlParameter("?ChannelId", channel_id ?? ""),
                new MySqlParameter("?DeviceId", deviceId ?? "")
            };
            var res = MySQLDataControl.ExecuteScalar(MySQLDataControl.GetConnection(), CommandType.Text, sqlStr, sqlParam);
            result = res != null ? res.ToString() : "";
            return result;
        }
    }
}